package de.baumann.browser.activitys.user

import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IAccountView
import de.baumann.browser.present.AccountPasswordPresenter
import de.baumann.browser.units.TimerUnit
import de.baumann.browser.views.dialog.Callback
import de.baumann.browser.views.dialog.VaptchaDialog
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 * @author：
 * @date：2018/10/18
 * @discription: 用户注册
 */
class RegisterActivity : BaseOdinActivity<IAccountView, AccountPasswordPresenter>(), IAccountView {


    private var timerUnit: TimerUnit? = null

    override val telNum: String
        get() = edRegisterTel.text.trim().toString()

    override val smsCode: String
        get() = edRegisterSmsCode.text.trim().toString()

    override val password: String
        get() = edRegisterPassword.text.trim().toString()

    override val otherCode: String
        get() = edRegisterInviteCode.text.trim().toString()

    override fun getLayout(): Int {
        return R.layout.activity_register
    }

    override fun initView() {

        initToolbar(commToolBar, null)
        tvGetSmsCode.setOnClickListener {
            VaptchaDialog(mContext,object :Callback{
                override fun callback(token: String) {
                    runOnUiThread {
                        timerUnit = TimerUnit(tvGetSmsCode)
                        timerUnit!!.startTime()
                        presenter.getSmsCode(token)
                    }
                }
            }).show()
        }
        btnRegister.setOnClickListener {
            presenter.register()
        }
    }

    override fun initData() {}

    override fun createPresenter(): AccountPasswordPresenter {
        return AccountPasswordPresenter()
    }

    override fun createView(): IAccountView {
        return this
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        endTimer()
    }

    override fun onError(errorMsg: String) {
        super.onError(errorMsg)
        endTimer()
    }

    private fun endTimer() {
        if (timerUnit != null) {
            timerUnit!!.endtTime()
        }
    }

    override fun requestSuccess() {
        showShortToast(getString(R.string.registration_success))
        finish()
    }

    override fun smsCodeSent() {
        showShortToast("验证码已发送")
    }

    override fun showBindEth(): Boolean {
        return false
    }
}
